
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = {
  plugins: [],
  module: {
    rules: []
  }
}
//防止package.json script语法错误出现多个空格
config.node = { fs: 'empty' }

//# 环境 development/product
config.mode = 'development';

//# 入口文件,可以有多个
config.entry = {
  main: [path.resolve(__dirname, 'src/main.js')]
}
//# 文件出口
config.output = {
  filename: 'bound.js',//* [name],[hash]...
  path: path.resolve(__dirname, 'dist'),
  publicPath: '' //* 公共路径
}
//# 文件引用路径简单化 自定义别名
config.resolve={
  extensions:['.vue','.js'],
  alias:{
    'vue$':'vue/dist/vue.esm.js',
    //'styles':path.resolve(__dirname,'src/assets/styles'),
    '@':path.resolve(__dirname,'src'),
    'assets':path.resolve(__dirname,'src/assets')
  }
}

//# Loader 文件处理器
//css-loader
config.module.rules.push(
  { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }
)
//stylus-loader
config.module.rules.push(
  { test: /\.stylus$/, use: ['style-loader', 'css-loader', 'postcss-loader','stylus-loader'] }
)
//字体图标
config.module.rules.push(
  { test: /\.(woff|svg|eot|ttf)\??.*$/, use: ['url-loader?name=fonts/[name].[md5:hash:hex:7].[ext]'] }
)
//图片文件
config.module.rules.push(
  {test:/\.(gif|jpg|png|jpeg|svg)$/,use:['url-loader']}
)

//js-loader
config.module.rules.push(
  {
    test:/\.js$/,
    use:[
      {loader:'babel-loader'}
    ],
    exclude:/node_modules/ //*排除目录
  }
)

//vue-loader
const VueLoaderPlugin = require('vue-loader/lib/plugin')
config.module.rules.push(
  {
    test:/\.vue$/,
    use:'vue-loader'
  }
)
config.plugins.push(
  new VueLoaderPlugin()
)



config.plugins.push(
  new HtmlWebpackPlugin({
    template: path.resolve(__dirname + '/src/index.html')
  })
)

//#region 本地服务器
config.devServer = {
  contentBase: path.resolve(__dirname, 'dist'), //默认访问路径
  overlay: true, //显示错误
  hot: true,  //热更新
  compress:true,  //开启虚拟服务时，为你的代码进行压缩。加快开发流程和优化
  port:9999,  //端口 默认8080
  host:'127.0.0.1',  //主机名 默认localhost
  //还有很多参数...
  /* 
  assetsSubDirectory:'static',
  assetsPublicPath:'/',
  proxyTable:{
    '/api':{
      target:'http://127.0.0.1:9999',
      pathRewrite:{
        '^/api':'/static/mock'
      }
    }
  }
  */
  //设置代理 将以/I/开头的数据请求重写为/Api/
  proxy:{
    '/I/':{
    target:'http://127.0.0.1:9999',
    pathRewrite:{ //重写的新路径，一般不做更改
      '^/I/':'/Api/' 
      }
    }
  }
}
config.plugins.push(
  new webpack.HotModuleReplacementPlugin(),
  new webpack.NoEmitOnErrorsPlugin()
)
//#endregion

module.exports = config;